package com.eight.clienthome.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eight.clienthome.dao.BottomArticleDao;
import com.eight.clienthome.service.BottomArticleService;
import com.eight.common.pojo.TbBottomArticle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 向文豪
 * @program: eight-education-parent
 * @description:
 * @date 2023/6/2 10:54
 */
@Service
public class BottomArticleServiceImpl implements BottomArticleService {

    @Autowired
    private BottomArticleDao bottomArticleDao;
    @Override
    public List<TbBottomArticle> findAll() {
        QueryWrapper<TbBottomArticle> wrapper = new QueryWrapper<>();

        wrapper.eq("is_disable",0);
        wrapper.eq("parent_id","0");
        List<TbBottomArticle> tbBottomArticles = bottomArticleDao.selectList(wrapper);

        List<TbBottomArticle> tbBottomArticles1 = bottomArticleDao.selectList(null);


        for(TbBottomArticle item : tbBottomArticles){
            List<TbBottomArticle> children = findChildren(item, tbBottomArticles1);
            item.setChildren(children);
        }
        return tbBottomArticles;
    }


    private List<TbBottomArticle> findChildren(TbBottomArticle item, List<TbBottomArticle> childrens){
        ArrayList<TbBottomArticle> list = new ArrayList<>();
        for (TbBottomArticle children : childrens) {
            if(item.getId().equals(children.getParentId())){
                list.add(children);
            }
        }
        for (TbBottomArticle eduSubject : list) {
            eduSubject.setChildren(findChildren(eduSubject,childrens));
        }
        return list;
    }
}
